<?php

class contJoinCt extends contController
{
    public $action = 'index';
    
    private $aCss = array(
                'name' => 'cont.join',
                'file_list' => array()
            );
    
    private $aJs = array(
                'name' => 'cont.join',
                'file_list' => array('lib/jquery.form', 'util/util.form.validator', 'util/spin', 'cont/cont.join')
            );
    
    public function index()
    {
        // 각 페이지에 필요한 CSS, JS 파일만 적용할 것.
        //$this->initialize();
        commViewer::_css($this->aCss);
        commViewer::_js($this->aJs);
    
        // 레이아웃 표시 여부
        //$this->sTopLayoutView = false;
        //$this->sAsideLayoutView = false;
        //$this->sFooterLayoutView = false;
        
        // 도시정보
        $aCityList = m('subway_info')->getCityList();
    
        // data assign
        $aAssignData = array(
            'aCityList' => $aCityList
        );
    
        // display view
        $this->view('/cont/join/index.html', $aAssignData);
    }
    
    /**
     * 
     * 회원가입
     */
    public function act()
    {
        $aParam = $this->params;
        
        // 등록 할 파일이 있을 경우
        if (empty($_FILES['filename']['name']) === false) {
            // 파일업로드
            $utilImages = new utilImages();
            //$utilImages->setPassiveModeFalse();
            $utilImages->setThumbnailMake(130, 130);
            $aFiles = $_FILES['filename'];
            $aResult = $utilImages->upload($aFiles, 'photo');
            if ($aResult[0] != 200) {
                utilData::jsonSingle(401);
                exit;
            }
            $aParam['upload_img'] = $aResult[1];
        }
        
        $aParam['user_birth'] = $aParam['b_year'].'-'.$aParam['b_month'].'-'.$aParam['b_day'];
        
        $bInsertId = m('user_info')->insert($aParam);
        if (empty($bInsertId)) {
            utilData::jsonSingle(201);
            exit;
        }
        
        // 회원번호
        $aParam['user_no'] = $bInsertId;
        
        // 관심역 등록
        if (empty($aParam['station1']) === false) {
            $aParam['station_no'] = $aParam['station1'];
            $bInsertId = m('user_interest_station')->insert($aParam);
        }
        if (empty($aParam['station2']) === false) {
            $aParam['station_no'] = $aParam['station2'];
            $bInsertId = m('user_interest_station')->insert($aParam);
        }
        
        utilData::jsonSingle(200);
    }
    
    /**
     * 
     * 이메일 중복확인
     */
    public function checkEmail()
    {
        $sUserEmail = $this->params['user_email'];
        
        $aInfo = m('user_info')->getEmailInfo($sUserEmail);
        if (empty($aInfo['user_no']) === false) {
            utilData::jsonSingle(201);
            exit;
        }
        
        utilData::jsonSingle(200);
    }
    
    /**
     * 
     * 호선 리스트
     */
    public function getLineList()
    {
        $iCityNo = $this->params['city_no'];
        
        $aLineList = m('subway_info')->getLineList($iCityNo);
        if (empty($aLineList)) {
            utilData::jsonSingle(201);
            exit;
        }
        
        utilData::jsonSingle(200, $aLineList);
    }
    
    /**
     * 
     * 역 리스트
     */
    public function getStationList()
    {
        $iCityNo = $this->params['city_no'];
        $iLineNo = $this->params['line_no'];
        
        $aStationList = m('subway_info')->getStationList($iCityNo, $iLineNo);
        if (empty($aStationList)) {
            utilData::jsonSingle(201);
            exit;
        }
        
        utilData::jsonSingle(200, $aStationList);
    }
}